package streaming.test.call;

import java.awt.Component;
import java.io.IOException;
import java.net.InetAddress;

import javax.media.ControllerEvent;
import javax.media.ControllerListener;
import javax.media.Manager;
import javax.media.NoPlayerException;
import javax.media.Player;
import javax.media.format.AudioFormat;
import javax.media.rtp.Participant;
import javax.media.rtp.ReceiveStream;
import javax.media.rtp.ReceiveStreamListener;
import javax.media.rtp.SessionAddress;
import javax.media.rtp.event.NewReceiveStreamEvent;
import javax.media.rtp.event.ReceiveStreamEvent;
import javax.media.rtp.rtcp.SourceDescription;
import javax.swing.JFrame;

import com.sun.media.rtp.RTPSessionMgr;

public class RTPReceiver extends JFrame implements ReceiveStreamListener, ControllerListener{

	
	RTPSessionMgr manager;
	Player player;
	
	public RTPReceiver(){
		manager = createManager("127.0.0.1", 22222, 1, true);
		
	}
	
	public RTPSessionMgr createManager(String address, int port, int ttl, boolean listener){
		RTPSessionMgr m = new RTPSessionMgr();
		
		if ( m == null ) return null;
		
		m.addFormat(new AudioFormat(AudioFormat.DVI_RTP, 22050, 4, 1), 17);
		
		if ( listener ) m.addReceiveStreamListener(this);
		
		String cname = m.generateCNAME();
		// get current user name
		String username = null;
		
		try {
			username = System.getProperty("user.name");
		} catch (SecurityException e){
			username = "rlm-user";
		}
		
		SessionAddress localAddress = new SessionAddress();
		
		try {
			InetAddress a = InetAddress.getByName(address);
			
			SessionAddress sessionAddress = new SessionAddress(a, port, a, port+1);
			
			SourceDescription userDesc[] = {
				new SourceDescription(SourceDescription.SOURCE_DESC_CNAME, cname, 1, false),
				new SourceDescription(SourceDescription.SOURCE_DESC_NAME, username, 1, false),
				new SourceDescription(SourceDescription.SOURCE_DESC_EMAIL, "test@jmf", 1, false)
			};
			
			m.initSession(localAddress, userDesc, 0.05, 0.25);
			
			m.startSession(sessionAddress, ttl, null);
			
			
		} catch (Exception e){
			return null;
		}
		
		return m;
	}
	
	private void showPLayer(String title){
		if(this.player == null) return;
		Component visualComponent = player.getVisualComponent();
		
		if(visualComponent != null){
			setTitle(title);
			setBounds(visualComponent.getBounds());
			getContentPane().add("Center", visualComponent);
			setVisible(true);
		}
		else{
			setVisible(false);
		}
		
	}
	
	@Override
	public void update(ReceiveStreamEvent e) {
		
		if(e instanceof NewReceiveStreamEvent){
			ReceiveStream stream = ((NewReceiveStreamEvent)e).getReceiveStream();
		
			Participant participant = stream.getParticipant();
			
			String cname;
			if(participant != null) 
				cname = participant.getCNAME();
			else{
				cname = "participant unknown";
			}
			
			try {
				this.player = Manager.createPlayer(stream.getDataSource());
				
				
				
			} catch (NoPlayerException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		
		
			if ( this.player == null ) return;
			
			this.player.addControllerListener(this);
			
			showPLayer(cname);
			
		}
		
	}

	@Override
	public void controllerUpdate(ControllerEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	
}
